package com.nacos.config.configuration;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;

import java.io.IOException;
import java.util.Properties;

/**
 * @author chn
 * @version 1.0
 * @Feature: 带Compare-And-Swap（CSA）的发布配置
 * @data 2024/9/12 14:40
 */
public class CSAConfiguration {
    public static void main(String[] args) throws NacosException, IOException {
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", "localhost:8848");

            ConfigService configService = NacosFactory.createConfigService(properties);
            NamingService namingService = NacosFactory.createNamingService(properties);

            // 首次发布配置，casMd5传入null
            boolean publishConfigCas = configService.publishConfigCas("CSA", "DEFAULT_GROUP", "spring.datasource.username=keke", null);
            System.out.println(publishConfigCas);

            // 发布成功之后，登录nacos查看md5值,记得md5值每次都会变
            String casMd5 = "67a2449b810ad4086f562ed5e15734aa";
            boolean configCas = configService.publishConfigCas("CSA", "DEFAULT_GROUP", "spring.datasource.password=020311", casMd5);
            System.out.println(configCas);


        }catch (NacosException e) {
            e.printStackTrace();
        }

    }
}
